package servlet.other;

import org.apache.commons.dbutils.QueryRunner;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@WebServlet(name = "ReportServlet", urlPatterns = "/wk07/ReportServlet")
@MultipartConfig
public class ReportServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //声明,这是一个html页面
        resp.setContentType("text/html; charset=UTF-8");

        HttpSession session = req.getSession();
        int power = Integer.parseInt(session.getAttribute("power").toString());
        //获取到文件的Id,方便改变其被举报的状态
        int fileId = Integer.parseInt(req.getParameter("fileId"));
        PrintWriter writer = resp.getWriter();

        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
            QueryRunner queryRunner = new QueryRunner();
            String sql = "UPDATE file SET is_delete= 1 WHERE id = " + fileId;
            int update = queryRunner.update(connection, sql);
            if (update > 0) {
                writer.println("举报成功！<br>");
            } else {
                writer.println("举报失败！<br>");
            }
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        writer.println("<a href=\"fileSystem.jsp?power=" + power + "\">返回上一页</a>\n");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);

    }
}
